package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.query.StrategyCatalogQuery;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.*;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("destination")
public class DestinationController {

    @Reference
    private IRegionService regionService;
    @Reference
    private IDestinationService  destinationService;
    @Reference
    private IStrategyCatalogService strategyCatalogService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private ITravelService travelService;

    @RequestMapping("")
    public String index(Model model){
        List<Region> regions = regionService.queryHotRegions();
        model.addAttribute("hotRegions", regions);
        return "/destination/index";
    }

    //查询目的地数据并且数据拆分放进模板里面
    @RequestMapping("getHotDestByRegionId")
    public String getHotDestByRegionId(Long regionId, Model model){
        //通过id查询目的地
        List<Destination> list = destinationService.getDestByRegionId(regionId);
        //拆分数据放进leftDests
        List<Destination> leftDests = list.subList(0, list.size() / 2);
        //拆分数据rightDests
        List<Destination> rightDests = list.subList(list.size() / 2 + 1, list.size());
        model.addAttribute("leftDests", leftDests);
        model.addAttribute("rightDests", rightDests);
        model.addAttribute("regionId", regionId);
        return "/destination/hotdestTpl";
    }

    //面包屑导航
    @RequestMapping("guide")
    public String guide(Long id, Model model){
        //通过递归方法看parentId是否为null，不是的话一直查询上级id
        List<Destination> toasts = destinationService.getToasts(id);
        //dest
        Destination dest = toasts.remove(toasts.size() - 1);
        //catalogs
        List<StrategyCatalog> catalogs = strategyCatalogService.getCatalogByDestId(id);
        model.addAttribute("catalogs", catalogs);
        model.addAttribute("toasts", toasts);
        model.addAttribute("dest", dest);
        //strategyDetails
        List<StrategyDetail> strategyDetails = strategyDetailService.getViewnumTop3(id);
        model.addAttribute("strategyDetails",strategyDetails);
        return "/destination/guide";
    }

    @RequestMapping("surveyPage")
    public String surveyPage(Model model, @ModelAttribute("qo") StrategyCatalogQuery qo){
        //toasts
        //通过递归方法看parentId是否为null，不是的话一直查询上级id
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        //dest
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts", toasts);
        model.addAttribute("dest", dest);
        return "/destination/survey";
    }

    @RequestMapping("survey")
    public String survey(Model model, @ModelAttribute("qo") StrategyCatalogQuery qo){
        //catalogs
        List<StrategyCatalog> catalogs = strategyCatalogService.getCatalogByDestId(qo.getDestId());
        model.addAttribute("catalogs",catalogs);
        StrategyCatalog catalog = null;
        if (catalogs != null && catalogs.size() > 0){
            //遍历catalogs拿到当前的攻略分类
            for (StrategyCatalog c : catalogs) {
                //catalog
                if (c.getId() == qo.getCatalogId()){
                    catalog =c;
                    model.addAttribute("catalog", c);
                }
            }
        }
        //detail
        if (catalog != null){
            List<StrategyDetail> details = catalog.getDetails();
            if (details != null && details.size() > 0){
                //拿第一个的数据
                StrategyDetail detail = details.get(0);
                StrategyContent content = strategyDetailService.getContentById(detail.getId());
                detail.setStrategyContent(content);
                model.addAttribute("detail", detail);
            }
        }
        return "/destination/surveyTpl";
    }

    @RequestMapping("travels")
    public String travels(@ModelAttribute("qo") TravelQuery qo, Model model){
        PageInfo pageInfo = travelService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "/destination/travelTpl";
    }
}
